package com.example.imagereversecolor

import android.graphics.Color
import android.graphics.ColorMatrix
import android.graphics.ColorMatrixColorFilter
import android.graphics.PorterDuff
import android.os.Bundle
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*


class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        button1.setOnClickListener { changeColor(PorterDuff.Mode.CLEAR) }
        button2.setOnClickListener { changeColor(PorterDuff.Mode.SRC) }
        button3.setOnClickListener { changeColor(PorterDuff.Mode.SRC_OVER) }
        button4.setOnClickListener { changeColor(PorterDuff.Mode.SRC_IN) }
        button5.setOnClickListener { changeColor(PorterDuff.Mode.SRC_OUT) }
        button6.setOnClickListener { changeColor(PorterDuff.Mode.SRC_ATOP) }
        button7.setOnClickListener { changeColor(PorterDuff.Mode.DST_OVER) }
        button8.setOnClickListener { changeColor(PorterDuff.Mode.DST_IN) }
        button9.setOnClickListener { changeColor(PorterDuff.Mode.DST_OUT) }
        button10.setOnClickListener { changeColor(PorterDuff.Mode.DST_ATOP) }
        button11.setOnClickListener { changeColor(PorterDuff.Mode.XOR) }
        button12.setOnClickListener { changeColor(PorterDuff.Mode.DARKEN) }
        button13.setOnClickListener { changeColor(PorterDuff.Mode.LIGHTEN) }
        button14.setOnClickListener { changeColor(PorterDuff.Mode.MULTIPLY) }
        button15.setOnClickListener { changeColor(PorterDuff.Mode.SCREEN) }
        button16.setOnClickListener { changeColor(PorterDuff.Mode.ADD) }
        button17.setOnClickListener { changeColor(PorterDuff.Mode.OVERLAY) }
        button18.setOnClickListener { changeColor(PorterDuff.Mode.DST) }
        button19.setOnClickListener { reverseColor(imageView) }
        button20.setOnClickListener { filterBlueLight(imageView) }
        resumeBtn.setOnClickListener { resumeColor() }
    }

    /**
     * 反色
     */
    private fun reverseColor(view: ImageView) {
        val cm = ColorMatrix(
            floatArrayOf(
                -1f, 0f, 0f, 0f, 255f,
                0f, -1f, 0f, 0f, 255f,
                0f, 0f, -1f, 0f, 255f,
                0f, 0f, 0f, 1f, 0f
            )
        )
        view.colorFilter = ColorMatrixColorFilter(cm)
    }

    /**
     * 过滤蓝光
     */
    private fun filterBlueLight(view: ImageView) {
        // 把蓝色减弱为原来的0.7
        val cm = ColorMatrix(
            floatArrayOf(
                1f, 0f, 0f, 0f, 0f,
                0f, 1f, 0f, 0f, 0f,
                0f, 0f, 0.7f, 0f, 0f,
                0f, 0f, 0f, 1f, 0f
            )
        )
        view.colorFilter = ColorMatrixColorFilter(cm)
    }

    private fun changeColor(mode: PorterDuff.Mode) {
        imageView.setColorFilter(Color.parseColor("#00FF00"), mode)
    }

    /**
     * 恢复
     */
    private fun resumeColor() {
        imageView.setColorFilter(Color.TRANSPARENT)
    }
}